package dao;

import entity.Books;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BooksDaoImpl extends BaseDao{

    /**
     * 查看列表
     * @return
     */
    public List<Books> list(){
        List<Books> list=new ArrayList<>();
        try{
            conn=getConnection();
            pstmt=conn.prepareStatement("select * from bookmanage");
            rs=pstmt.executeQuery();
            while (rs.next()){
                Books books=new Books();
                books.setId(rs.getInt("id"));
                books.setName(rs.getString("name"));
                books.setAuthor(rs.getString("author"));
                books.setTime(rs.getString("time"));
                books.setType(rs.getInt("type"));
                list.add(books);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            closeAll(conn,pstmt,rs);
        }
        return list;
    }

    /**
     * 删除
     * @param Id
     * @return
     */
    public int delete(int Id){
        int iRet=0;
        conn=getConnection();
        try{
            String sql="delete from bookmanage where id=?";
            pstmt=conn.prepareStatement(sql);
            pstmt.setInt(1,Id);
            iRet=pstmt.executeUpdate();
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            closeAll(conn,pstmt,rs);
        }
        return iRet;
    }

    /**
     *
     * @param books
     * @return
     */
    public int add(Books books){
        int iRet=0;
        conn=getConnection();
        try{
            String sql="INSERT INTO `bookmanage`(`name`, `author`, `time`, `type`) VALUES (?,?,?,?)";
            pstmt=conn.prepareStatement(sql);
            pstmt.setString(1,books.getName());
            pstmt.setString(2,books.getAuthor());
            pstmt.setString(3,books.getTime());
            pstmt.setInt(4,books.getType());
            iRet=pstmt.executeUpdate();
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            closeAll(conn,pstmt,rs);
        }
        return iRet;
    }
}

